using Precise.Products;
using Abp.IdentityServer4;
using Abp.Zero.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Precise.Authorization.Roles;
using Precise.Authorization.Users;
using Precise.Chat;
using Precise.Editions;
using Precise.Friendships;
using Precise.MultiTenancy;
using Precise.MultiTenancy.Accounting;
using Precise.MultiTenancy.Payments;
using Precise.Storage;

namespace Precise.EntityFrameworkCore
{
    public class PreciseDbContext : AbpZeroDbContext<Tenant, Role, User, PreciseDbContext>, IAbpPersistedGrantDbContext
    {
        public virtual DbSet<Goods> Goods { get; set; }

        public virtual DbSet<GoodsImage> GoodsImag { get; set; }

        public virtual DbSet<Product> Product { get; set; }

        /* Define an IDbSet for each entity of the application */

        //Add-Migration


        public virtual DbSet<BinaryObject> BinaryObjects { get; set; }

        public virtual DbSet<Friendship> Friendships { get; set; }

        public virtual DbSet<ChatMessage> ChatMessages { get; set; }

        public virtual DbSet<SubscribableEdition> SubscribableEditions { get; set; }

        public virtual DbSet<SubscriptionPayment> SubscriptionPayments { get; set; }

        public virtual DbSet<Invoice> Invoices { get; set; }

        public virtual DbSet<PersistedGrantEntity> PersistedGrants { get; set; }

        public PreciseDbContext(DbContextOptions<PreciseDbContext> options)
            : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

           
           
           
            modelBuilder.Entity<GoodsImage>(g =>
            {
                g.HasIndex(e => new { e.TenantId });
            });
 modelBuilder.Entity<Goods>(g =>
            {
                g.HasIndex(e => new { e.TenantId });
            });
 modelBuilder.Entity<Product>(p =>
            {
                p.HasIndex(e => new { e.TenantId });
            });
 modelBuilder.Entity<BinaryObject>(b =>
            {
                b.HasIndex(e => new { e.TenantId });
            });

            modelBuilder.Entity<ChatMessage>(b =>
            {
                b.HasIndex(e => new { e.TenantId, e.UserId, e.ReadState });
                b.HasIndex(e => new { e.TenantId, e.TargetUserId, e.ReadState });
                b.HasIndex(e => new { e.TargetTenantId, e.TargetUserId, e.ReadState });
                b.HasIndex(e => new { e.TargetTenantId, e.UserId, e.ReadState });
            });

            modelBuilder.Entity<Friendship>(b =>
            {
                b.HasIndex(e => new { e.TenantId, e.UserId });
                b.HasIndex(e => new { e.TenantId, e.FriendUserId });
                b.HasIndex(e => new { e.FriendTenantId, e.UserId });
                b.HasIndex(e => new { e.FriendTenantId, e.FriendUserId });
            });

            modelBuilder.Entity<Tenant>(b =>
            {
                b.HasIndex(e => new { e.SubscriptionEndDateUtc });
                b.HasIndex(e => new { e.CreationTime });
            });

            modelBuilder.Entity<SubscriptionPayment>(b =>
            {
                b.HasIndex(e => new { e.Status, e.CreationTime });
                b.HasIndex(e => new { e.PaymentId, e.Gateway });
            });

            modelBuilder.ConfigurePersistedGrantEntity();
        }
    }
}
